/**
 * dxAudio - JavaScript wrapper for the native Audio module.
 *
 * This module provides a simple interface to control audio playback.
 * Capture functions are for testing purposes only.
 */
import * as native from './libvbar-m-dxaudio.so';

const dxAudio = {};

/**
 * Initialize Audio module and playback channel.
 * @param {object} config - Configuration object (Optional, reserved).
 */
dxAudio.init = function (config = {}) {
    native.init(config);
};

/**
 * Uninitialize Audio module.
 */
dxAudio.deinit = function () {
    native.deinit();
};

/**
 * Play a WAV file.
 * @param {string} path - Path to the WAV file.
 */
dxAudio.playWav = function (path) {
    if (typeof path !== 'string') {
        throw new Error("dxAudio.playWav: path must be a string");
    }
    native.playWavFile(path);
};

/**
 * Play WAV data from ArrayBuffer.
 * @param {ArrayBuffer} data - Raw WAV file data.
 */
dxAudio.playWavData = function (data) {
    if (!(data instanceof ArrayBuffer)) {
        throw new Error("dxAudio.playWavData: data must be an ArrayBuffer");
    }
    native.playWavData(data);
};

/**
 * Play TTS (Text To Speech).
 * @param {string} text - Text to speak.
 */
dxAudio.playTts = function (text) {
    if (typeof text !== 'string') {
        throw new Error("dxAudio.playTts: text must be a string");
    }
    // Driver support pending
    native.playTts(text);
};

/**
 * Set playback volume.
 * @param {number} volume - Volume level (0-100).
 */
dxAudio.setVolume = function (volume) {
    if (typeof volume !== 'number') {
        throw new Error("dxAudio.setVolume: volume must be a number");
    }
    native.setVolume(volume);
};

/**
 * Stop current playback.
 */
dxAudio.stop = function () {
    native.stop();
};

/**
 * [TEST ONLY] Initialize audio capture.
 * This function requests a capture channel and sets up a test callback.
 */
dxAudio.testInitCapture = function () {
    native.initCapture();
};

/**
 * [TEST ONLY] Start or stop audio capture.
 * @param {boolean} enable - True to start capture, False to stop.
 */
dxAudio.testSetCaptureStatus = function (enable) {
    if (typeof enable !== 'boolean') {
        throw new Error("dxAudio.testSetCaptureStatus: enable must be a boolean");
    }
    native.setCaptureStatus(enable);
};

export default dxAudio;